package com.changgou.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.changgou.goods.feign.CategoryFeign;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.item.service.PageService;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wzw
 * @Date: 2020/12/18 23:59
 * @version: 1.8
 */
@Service
public class PageServiceImpl implements PageService {

    //注入模板对象
    @Autowired
    private TemplateEngine templateEngine;

    //注入SkuFeign远程调用
    @Autowired(required = false)
    private SkuFeign skuFeign;

    //注入SpuFeign远程调用
    @Autowired(required = false)
    private SpuFeign spuFeign;

    //注入CategoryFeign远程调用
    @Autowired(required = false)
    private CategoryFeign categoryFeign;

    //注入yml文件中对应属性的内容
    @Value("${pagepath}")
    private String pagepath;

    /**
     * @author wzw
     * 生成静态页
     * @Date 0:21 2020/12/19
     * @param spuId
     * @return void
    **/
    @Override
    public void createHtml(Long spuId) {

        try {
            //1.获取静态页面化技术数据
            // 自定义方法:获取模板需要的数据
            Map<String, Object> dataModel = getDataModel(spuId);
            //设置静态页面参数:构建模板数据context
            Context context = new Context();
            //获取数据
            context.setVariables(dataModel);

            //2.2设置静态页面参数:生成的位置:参数3=>writer
            //new文件,并指定路径(路径在配置文件中,这里直接要value注入了)
            File dir = new File(pagepath);
            //空判断
            if (!dir.exists()) {
                //目录为空,创建多级目录
                dir.mkdirs();
            }
            // 拼接静态页面名称==id.html
            File dest = new File(dir, spuId + ".html");

            //3.生成页面
            PrintWriter writer = new PrintWriter(dest, "UTF-8");

            //4.实现功能:模板对象(模板,数据,输出)
            templateEngine.process("item", context, writer);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @author wzw
     * 获取模板需要的数据
     * @Date 0:03 2020/12/19
     * @param spuId
     * @return java.util.Map<java.lang.String,java.lang.Object>
    **/
    private Map<String, Object> getDataModel(Long spuId) {
        //创建存入数据的map
        HashMap<String, Object> dataModel = new HashMap<>();
        //1.实现功能:获取商品基本信息
        Result<Spu> spuResult = spuFeign.findById(spuId);
        //获取已查到的商品对象
        Spu spu = spuResult.getData();
        //添加到map中
        dataModel.put("spu", spu);

        //2.获取库存信息
        //创建对象
        Sku sku = new Sku();
        //封装条件
        sku.setSpuId(spuId);
        //实现功能:查询集合获取商品库存信息
        Result<List<Sku>> skuResult = skuFeign.findList(sku);
        //获取查询到的数据集合
        List<Sku> skuList = skuResult.getData();
        //存到map中
        dataModel.put("skuList", skuList);

        // 3.商品分类信息(一级,二级,三级)目录
        //实现查找一级目录功能
        Category category1 = categoryFeign.findById(spu.getCategory1Id()).getData();
        //实现查找二级目录功能
        Category category2 = categoryFeign.findById(spu.getCategory2Id()).getData();
        //实现查找三级目录功能
        Category category3 = categoryFeign.findById(spu.getCategory3Id()).getData();
        //添加到map中
        dataModel.put("category1", category1);
        dataModel.put("category2", category2);
        dataModel.put("category3", category3);

        // 4.商品小图(以','号分割)
        String[] imageList = spu.getImages().split(",");
        // 添加到map中
        dataModel.put("imageList", imageList);

        // 5.商品规格
        Map<String, String> specificationList = JSON.parseObject(spu.getSpecItems(), Map.class);
        //添加到map中
        dataModel.put("specificationList", specificationList);

        // 6.返回map结果集
        return dataModel;
    }
}
